package thread;

public class Printab {
	public static void main(String[] args) {
		Thread th1 = new mythread1();
		th1.start();
		Thread th2 = new mythread1();
		th2.start();
		Thread th3 = new mythread1();
		th3.start();
	}
}

class mythread1 extends Thread {
	private static String str = "C";
	private static int i = 0;

	public mythread1() {
	}

	public synchronized void run() {
		try {
			while (i < 10) {
				synchronized (Printab.class) {
					if (str.endsWith("C")) {
						System.out.println("A");
						str = str + "A";
						System.out.println("str=" + str);
					}
					if (str.endsWith("A")) {
						System.out.println("B");
						str = str + "B";
					}
					if (str.endsWith("B")) {
						System.out.println("C");
						str = str + "C";
						i++;
					}
				}
			}
		} catch (Exception e) {
		}
	}
}
